package com.gitee.wsl.ext.string

import kotlin.math.min
import kotlin.random.Random





/**
 * Randomly change case for the letters in a string
 *
 * @param rnd   random to be used
 * @param input input string to randomize
 * @return string with random characters changed case.
 */
fun String.randomlyChangeCase(rnd: Random): String {
    val sb = StringBuilder(this)
    for (i in sb.indices) {
        val currentChar = sb[i]
        if (currentChar.isUpperCase() && rnd.nextBoolean()) {
            sb[i] = currentChar.lowercaseChar()
        } else if (currentChar.isLowerCase() && rnd.nextBoolean()) {
            sb[i] = currentChar.uppercaseChar()
        }
    }
    return sb.toString()
}


internal fun _containsControlAscii(s: String): Boolean {
    var i = 0
    val n = s.length
    while (i < n) {
        val c = s.get(i)
        if (c.code in 0..31) return true
        i++
    }
    return false
}


internal fun containsAnyChar(str: CharSequence, chars: String): Boolean {
    return indexOfAnyChar(str, chars) >= 0
}

private fun indexOfAnyChar(str: CharSequence?, chars: String?): Int {
    if (str == null || str.length <= 0) return -1
    if (chars == null || chars.length <= 0) return -1
    var i = 0
    val n = str.length
    while (i < n) {
        if (chars.indexOf(str.get(i)) >= 0) return i
        i++
    }
    return -1
}

